<template>
  <div class="app-container">
    <!--    <el-button @click="fetchData" class="el-button&#45;&#45;primary">refresh</el-button>-->

    <!-- 条件查询 -->
    <el-form
      :inline="true"
      :model="query"
      class="query-form-inline">
      <el-form-item>
        <el-input v-model="query.name" placeholder="讲师名"/>
      </el-form-item>
      <el-form-item label="level">
        <el-select v-model="query.level" clearable placeholder="讲师头衔">
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="query.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="query.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="default" @click="resetQuery">清除</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData" icon="el-icon-search">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table
      :data="list"
      v-loading="listLoading"
      element-loading-text="数据加载中"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      fit
      border
      size="mini"
      highlight-current-row
      style="width: 100%">
      <el-table-column
        label="Index"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <!--      <el-table-column label="Index" type="index" width="80"></el-table-column>-->
      <el-table-column prop="id" label="Id" width="150"></el-table-column>
      <el-table-column prop="name" label="姓名" width="80"></el-table-column>
      <el-table-column
        label="头衔"
        width="80">
        <template slot-scope="scope">
          {{ scope.row.level === 1 ? '高级讲师' : '首席讲师' }}
        </template>
      </el-table-column>
      <el-table-column label="资历" prop="intro"></el-table-column>
      <el-table-column label="添加时间" prop="gmtCreate" width="136"></el-table-column>
      <el-table-column label="排序" prop="sort" width="80"></el-table-column>
      <!--      <el-table-column label="操作" width="200" align="center"></el-table-column>-->
      <!--      <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>-->
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <router-link :to="`/teacher/edit/${scope.row.id}`">
            <el-button size="mini" icon="el-icon-edit">编辑</el-button>
          </router-link>
          <el-button
            @click="handleDelete(scope.row.id)"
            size="mini"
            icon="el-icon-delete"
            style="margin-left: 6px"
            type="danger">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[3, 5, 8, 10, 20, 50, 100]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :hide-on-single-page="hideOnSinglePage"
      background
      style="padding: 30px 0; text-align: center"
      :total="total">
    </el-pagination>

    <!--    <el-table
          v-loading="listLoading"
          :data="list"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
        >
          <el-table-column align="center" label="Index" width="95">
            <template slot-scope="scope">
              {{ scope.$index }}
            </template>
          </el-table-column>
          <el-table-column label="Id">
            <template slot-scope="scope">
              {{ scope.row.id }}
            </template>
          </el-table-column>
          <el-table-column label="Name">
            <template slot-scope="scope">
              {{ scope.row.name }}
            </template>
          </el-table-column>
        </el-table>-->
  </div>
</template>

<script>
import ts from '@/api/edu/teacher'

export default {
  data() {
    return {
      page: 1,
      limit: 8,
      total: 0,
      query: {},
      list: null,
      listLoading: true,
      hideOnSinglePage: false,
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      ts.getListPage(this.page, this.limit, this.query)
        .then((response) => {
          console.log(response)
          this.total = response.data.total
          this.list = response.data.items
          this.listLoading = false
        })
        .catch(e => {
          console.log(e)
        })
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.limit = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val
      this.fetchData()
    },

    resetQuery() {
      this.query = {}
      this.fetchData()
    },

    handleDelete(id) {
      this.$confirm('确认删除?', '删除讲师', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ts.deleteById(id)
          .then((response) => {
            console.log(response)
            this.$message({
              showClose: true,
              message: '删除成功: ' + id,
              type: 'success',
              duration: 3*1000
            })
            this.fetchData()
          })
      })
    }
  },
  watch: {
    query: {
      handler(val) {
        this.fetchData()
      },
      deep: true
    }
  }
}
</script>
